/* 
** 部门/人员审核列表数据处理
*/
import {auditKind, dataOperation} from '@/components/ivew_conf/auditing/auditing_type_conf'


export default (ctx, next) =>{

    // 设置默认值 防止无数据时 返回为null
    let resData = ctx.params || []; 
    let funs = [statueKey, formDataParams, buildAuditingDescribe, buildPersonlInof];
    
    resData.forEach(item =>{

        funs.forEach(fn =>{
            fn(item)
        })
        
    })    

    next()
}


// 通过审核结果,判断信息审核状态, 当指为空时表示 “ 未审核 ”
function statueKey(data) {

    let key = data['result'] || '等待审核';
    
    data['result'] = key === '审核不通过' ? '审核驳回' : key;
}

//解析审核数据
function formDataParams(data){

    try{
        data['jsonData'] = JSON.parse(data['jsonData'])
    }catch{
        console.log(`审核数据错误`, data)
    }
   
}

// 拼接申请描述
function buildAuditingDescribe(data){
    let action = dataOperation[data['action']]
    let type = auditKind[data['kind']];

    data['_describe'] = {

        action,
        type
            
    };

    
}

// 格式化人员信息
function buildPersonlInof(data){

    let  personal = {
        
        // 申请人
        createdBy: data['createdBy'],

        // 审核人
        checkedBy: data['checkedBy']

    };

    // 解析人员信息
    Object.keys(personal).forEach(key =>{

        let item = personal[key].split(':');

        data[key] = {
            company: item[0] || '',
            department: item[1] || '',
            nickname: item[2] || '',
            phone: item[3] || ''
        }
        
    })
    

}